package ps.client.plugin.eq2.log;

import java.util.regex.Pattern;



public class LogLine {
	
	public static final int TYPE_NONE = -1;
	public static final int TYPE_CHAT = 1;
    
    public static final int OFFSET_CONTENT = 39;
	
	public static final Pattern PATTERN_TIME_STAMP = Trigger.TIME_STAMP.getPattern();
	public static final Pattern PATTERN_CHAT = Trigger.CHAT.getPattern();
	
	String line;
	int secs = 0;
	String dateStr;
	String timeStr;
	String content;
	
	ChatLine chatLine;
	
	int type = TYPE_NONE;
	
	LogParser logParser;
	
	String attacker = null;
	String victim = null;
	
	public LogLine(LogParser logParser, String line) {
		this.logParser = logParser;
		this.line = line;
		scan();
	}
	
	void scan() {
		if (PATTERN_TIME_STAMP.matcher(line).matches()) {
			secs = Integer.parseInt(line.substring(1, 11));
			dateStr = line.substring(13, 37);
			timeStr = dateStr.substring(11, 19);
			content = line.substring(OFFSET_CONTENT);
			
			if (PATTERN_CHAT.matcher(content).matches()) {
                chatLine = new ChatLine(this);
                type = TYPE_CHAT;
			}
		} else {
			content = line;
		}
	}
	
	public String getLine() {
	    return line;
	}
	
	public int getType() {
		return type;
	}
	
	public ChatLine getChatLine() {
		return chatLine;
	}
	
	public String getContent() {
		return content;
	}
	
	public String getAvatarName() {
		return logParser != null ? logParser.getCurrentAvatarName() : "*";
	}
	
	public String getAvatarServer() {
		return logParser != null ? logParser.getCurrentAvatarServer() : "*";
	}
	
	public boolean checkTrigger(String[] trigger) {
		for (int i = 0; i < trigger.length; i++) {
			if (line.matches(trigger[i])) {
				return true;
			}
		}
		return false;
	}
	
	public String getAttacker() {
		if (attacker == null) {
			if (type != TYPE_CHAT) {
				int index = content.indexOf(" has killed");
				index = index < 0 ? content.indexOf(" have killed") : index;
				if (index > 0) {
					attacker = content.substring(0, index);
				}
			}
		}
		return attacker;
	}
	
	public String getVictim() {
		if (victim == null) {
			if (type != TYPE_CHAT) {
				int index = content.indexOf(" has killed");
				index = index < 0 ? content.indexOf("have killed") : index;
				if (index >= 0 && content.length() > index + 13) {
					victim = content.substring(index + 12, content.length() - 1);
				}
			}
		}
		return victim;
	}
	
	public String toString() {
		return line;
	}
	
	public static void main(String[] args) throws Exception {
		LogLine logLine = new LogLine(null, "(1243101493)[Sat May 23 19:58:13 2009] Sornnn has killed a skeletal guardian.");
		System.out.println("line: " + logLine.line);
		System.out.println("secs: " + logLine.secs);
		System.out.println("dateStr: " + logLine.dateStr);
		System.out.println("timeStr: " + logLine.timeStr);
		System.out.println("content: " + logLine.content);
		System.out.println("attacker: " + logLine.getAttacker());
		System.out.println("victim: " + logLine.getVictim());
		System.out.println("chat line: " + (logLine.getType() == TYPE_CHAT));
	}
}
